//
//  CHItemSelectView.swift
//  health
//
//  Created by cyptt on 2023/5/29.
//

import UIKit

public class CHItemSelectView: UIView {

    
   
    private var marginLeftW:CGFloat = 0
    private var marignRightW:CGFloat = 0
    private var isLineRight:Bool = false
    private var mIsShowLeftImg:Bool = false
    private var mIsShowRightImg:Bool = true
    
    
    private var leftImage_W:CGFloat = 0 //左边图片宽度
    private var rightImage_W:CGFloat = 0 //右边图片宽度
    
    private  var selectItemCallback:((_ leftTitle:String) -> ())?
    
    
    public lazy var leftImageV = {()-> UIImageView in
        let imageV:UIImageView = UIImageView.init()
        imageV.image = UIImage.init(named: "check_box_selected", in: CHBundle.bundle(), compatibleWith: nil)
        
        imageV.contentMode = .scaleAspectFit
        
        imageV.isHidden = true
        return imageV
    }()
   public lazy var leftTitle = {()-> UILabel in
        let lab:UILabel = UILabel.init()
        lab.textColor = UIColor.init(red: 48/255.0, green: 47/255.0, blue: 53/255.0, alpha: 1)

        return lab
    }()
    public lazy var rightImageV = {()-> UIImageView in
        let imageV:UIImageView = UIImageView.init()
        imageV.image = UIImage.init(named: "arrow_gray_right_chsimple", in: CHBundle.bundle(), compatibleWith: nil)
        

        imageV.contentMode = .scaleAspectFit
        return imageV
    }()
    
    public lazy var rightTitle = {()-> UILabel in
        let lab:UILabel = UILabel.init()
        lab.textColor = UIColor.init(red: 48/255.0, green: 47/255.0, blue: 53/255.0, alpha: 1)

        return lab
    }()
    public lazy var lineView = {()-> UIView in
        let lineView:UIView = UIView.init()
        lineView.backgroundColor = UIColor.init(red: 216/255.0, green: 216/255.0, blue: 216/255.0, alpha: 1)
        return lineView
    }()
    
    
    public lazy var rightLineView = {()-> UIView in
        let lineView:UIView = UIView.init()
        lineView.backgroundColor = UIColor.init(red: 216/255.0, green: 216/255.0, blue: 216/255.0, alpha: 1)

        lineView.isHidden = true
        return lineView
    }()
    


    /*
     @param marginLW  距离左右距离
     @param marginRW   距离右边距离
     @param isLineRight  线是否
     @param isShowLeftImg 是否显示左边图片，默认不现实
     @param isShowRightImg 是否显示右边图片，默认显示
     */
    
    public init(marginLW:CGFloat = 20,marginRW:CGFloat = 20,isLineRight:Bool = false,isShowLeftImg:Bool = false,isShowRightImg:Bool = true){
        
        super.init(frame: CGRect.zero)
        
        self.marginLeftW = marginLW
        self.marignRightW = marginRW
        self.isLineRight = isLineRight
        self.mIsShowLeftImg = isShowLeftImg
        self.mIsShowRightImg = isShowRightImg
        setUI()
    }
   private override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func selectItemCallback(closure:@escaping(_ leftTitle:String)-> Void){
     
       self.selectItemCallback = closure
    }
    
    
    public  func refreshRightTitle(title:String){
        self.rightTitle.text = title
        
        self.rightTitle.snp.makeConstraints { make in
            make.right.equalTo(self.rightImageV.snp.left).offset(5)
            make.centerY.equalToSuperview()
        }
    }
    func setUI(){


        
        self.addSubview(self.leftImageV)
        self.addSubview(self.leftTitle)
        self.addSubview(self.lineView)
        self.addSubview(self.rightImageV)
 
        self.addSubview(self.rightTitle)
        
        self.addSubview(self.rightLineView)
      
     
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapClick(tap: )))
        self.addGestureRecognizer(tap)

        
        //显示右边线
        if self.isLineRight == true{
            self.rightLineView.isHidden = false
            
        }
        
       
        
       
        
    }
    
    public override func layoutSubviews() {
        
       
        //显示左边图片
        if self.mIsShowLeftImg == true{
            self.leftImageV.isHidden = false
            
            //先计算图片宽高
            
            let leftImage:UIImage? = self.leftImageV.image
        
            self.leftImage_W = leftImage?.size.width ?? 0
        }
        
        //显示右边图片
        if self.mIsShowRightImg == true{
            self.rightImageV.isHidden = false
            
            //先计算图片宽高
            let rightImage:UIImage? = self.rightImageV.image
        
            self.rightImage_W = rightImage?.size.width ?? 0
            
        }else{
            self.rightImageV.isHidden = true
        }
        
        setPosition()
    
        
    }
    
    
   
}

//MARK: --btnClick
extension CHItemSelectView{
  
    

    @objc func tapClick(tap:UITapGestureRecognizer){
        
        
        if self.selectItemCallback != nil{
            self.selectItemCallback!(self.leftTitle.text ?? "")
        }
    }
}



//MARK: ---position
extension CHItemSelectView{
    
   
    func setPosition(){
        
        
        
        self.leftImageV.snp.makeConstraints { make in
            make.left.equalTo(self).offset(self.marginLeftW)
            make.centerY.equalTo(self)
            make.width.equalTo(self.leftImage_W)
     
        }
        
        //leftImageV 宽度最多20
    
        self.leftTitle.snp.makeConstraints { make in

            var marginLW = self.marginLeftW
            if self.mIsShowLeftImg == false {
                marginLW = self.marginLeftW
            }else{
                marginLW = self.marginLeftW + self.leftImage_W + 8
            }
            
            make.left.equalTo(self).offset(marginLW)
            make.top.equalTo(self).offset(0)
            make.bottom.equalToSuperview()
        }
        
        self.rightImageV.snp.makeConstraints { make in
            make.right.equalTo(self).offset(-self.marignRightW)
            make.centerY.equalTo(self)
            make.width.equalTo(self.rightImage_W)
        }
        
        self.lineView.snp.makeConstraints { make in
            make.left.equalTo(self).offset(self.marginLeftW)
            make.right.equalToSuperview().offset(-self.marignRightW)
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        self.rightTitle.snp.makeConstraints { make in
       
            var titleMarginRw = 0
            if self.mIsShowRightImg == true{
                titleMarginRw = -8
            }else{
                titleMarginRw = 0
            }
            make.right.equalTo(self.rightImageV.snp.left).offset(titleMarginRw)
            make.centerY.equalToSuperview()
        }
        
        self.rightLineView.snp.makeConstraints { make in
            make.left.equalTo(self.lineView.snp.right)
            make.top.equalTo(self.lineView.snp.top)
            make.right.equalToSuperview()
            make.height.equalTo(self.lineView)
        }
        
        
        
        

    }
}
